<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="560px"
    append-to-body
    @close="closeDialog"
    :close-on-click-modal="false"
  >
    <el-form
      :model="info"
      :rules="rules"
      ref="queryForm"
      label-width="120px"
      size="small"
      class="pr40"
    >
      <el-form-item label="文件名称" prop="docName">
        <el-input v-model="info.docName" clearable style="width: 100%" />
      </el-form-item>
      <el-form-item label="文件分类" prop="classifyId">
        <el-select v-model="info.classifyId" disabled style="width: 100%">
          <el-option :label="classifyObj ? classifyObj.name : ''" :value="info.classifyId" />
        </el-select>
      </el-form-item>
      <el-form-item label="模板选择" prop="templateId">
        <el-select
          v-model="info.templateId"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in templateList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="截止时间" prop="deadlineTime">
        <el-date-picker
          v-model="info.deadlineTime"
          type="datetime"
          placeholder="选择日期时间"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer pr40">
      <el-button size="small" @click="open = false">取消</el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="handleSubmit"
        size="small"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>
<script>
import { addDocInfo, editDocInfo } from "@/api/business";
import { getTemplateAll } from "@/api/business";
import { Message } from "element-ui";
import { cloneDeep } from "lodash";
import { getClassifyObj } from '@/utils/index'

export default {
  name: "DocEditModal",
  inject: ['getTreeData'],
  computed: {
    treeData() {
      return this.getTreeData()
    }
  },
  props: {
    editData: {
      type: Object,
      default: () => ({}),
    },
    visible: {
      type: Boolean,
      default: false,
    },
    selectedNode: {
      type: Object,
      default: () => ({}),
    },
  },
  watch: {
    visible(val) {
      this.open = val;
      if (val) {
        this.$nextTick(() => {
          this.$refs.queryForm.clearValidate();
          this.title = this.editData?.id ? "编辑文件" : "新增文件";
        })
      }
    },
    editData(val) {
      this.info = cloneDeep(val);
      this.info.classifyId = this.info.classifyId || this.selectedNode.id;
      if (this.selectedNode?.id) {
        this.classifyObj = this.selectedNode
      } else {
        this.classifyObj = getClassifyObj(this.treeData, this.info.classifyId)
      }
    },
  },
  data() {
    return {
      open: this.visible,
      info: this.editData,
      title: "新增文件",
      templateList: [],
      loading: false,
      rules: {
        docName: [
          { required: true, message: "请输入文件名称", trigger: "change" },
          { pattern: /\s*\S+?/, message: "请输入非空字符", trigger: "change" },
        ],
        templateId: [
          { required: true, message: "请选择模板", trigger: "change" },
        ],
        classifyId: [
          { required: true, message: "请选择文件分类", trigger: "change" },
        ],
        deadlineTime: [
          { required: true, message: "请选择截止时间", trigger: "change" },
        ],
      },
      classifyObj: {},
      now: null,
    };
  },
  mounted() {
    getTemplateAll().then((res) => {
      this.templateList = res.data;
    });
  },
  methods: {
    closeDialog() {
      this.$emit("update:visible", false);
      this.$emit("onClose");
    },
    handleSubmit() {
      this.$refs.queryForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          if (this.editData.id) {
            await editDocInfo(this.info);
          } else {
            await addDocInfo(this.info);
          }
          this.open = false;
          Message.success("保存成功");
          this.$emit("onSuccess");
          this.loading = false;
        }
      });
    },
  },
};
</script>
<style scoped>
.dialog,
.el-select-dropdown__item {
  font-family: "microsoft yahei";
}
</style>
